<template>
  <el-dialog title="监控设置" :visible.sync="dialogVisible" width="600px" @close="handleClose">
    <el-form :model="localSettings" label-width="120px">
      <el-form-item label="自动刷新间隔">
        <el-select v-model="localSettings.refreshInterval">
          <el-option label="5秒" :value="5000"></el-option>
          <el-option label="10秒" :value="10000"></el-option>
          <el-option label="30秒" :value="30000"></el-option>
          <el-option label="1分钟" :value="60000"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据保留天数">
        <el-input-number 
          v-model="localSettings.dataRetentionDays" 
          :min="1" 
          :max="365"
          controls-position="right">
        </el-input-number>
      </el-form-item>
      <el-form-item label="告警阈值设置">
        <el-switch v-model="localSettings.enableAlerts"></el-switch>
      </el-form-item>
      <el-form-item label="数据质量检查">
        <el-switch v-model="localSettings.enableQualityCheck"></el-switch>
      </el-form-item>
      <el-form-item label="自动导出">
        <el-switch v-model="localSettings.enableAutoExport"></el-switch>
      </el-form-item>
      <el-form-item label="导出格式" v-if="localSettings.enableAutoExport">
        <el-select v-model="localSettings.exportFormat">
          <el-option label="Excel (.xlsx)" value="xlsx"></el-option>
          <el-option label="CSV (.csv)" value="csv"></el-option>
          <el-option label="JSON (.json)" value="json"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSave">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "SettingsDialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    settings: {
      type: Object,
      default: () => ({
        refreshInterval: 5000,
        dataRetentionDays: 30,
        enableAlerts: true,
        enableQualityCheck: true,
        enableAutoExport: false,
        exportFormat: 'xlsx'
      })
    }
  },
  data() {
    return {
      localSettings: {}
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(value) {
        this.$emit('update:visible', value);
      }
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        // 对话框打开时，复制设置到本地
        this.localSettings = { ...this.settings };
      }
    },
    settings: {
      handler(newVal) {
        this.localSettings = { ...newVal };
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    handleClose() {
      this.$emit('close');
    },
    handleSave() {
      // 验证设置
      if (!this.validateSettings()) {
        return;
      }
      
      this.$emit('save', this.localSettings);
      this.handleClose();
    },
    validateSettings() {
      if (this.localSettings.refreshInterval < 1000) {
        this.$message.warning('刷新间隔不能小于1秒');
        return false;
      }
      
      if (this.localSettings.dataRetentionDays < 1 || this.localSettings.dataRetentionDays > 365) {
        this.$message.warning('数据保留天数必须在1-365天之间');
        return false;
      }
      
      return true;
    }
  }
};
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 20px;
}

.el-input-number {
  width: 200px;
}

.el-select {
  width: 200px;
}
</style>
